package com.wx.tnsenterprise.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.tnsenterprise.mapper.EnterpriseMapper;
import com.wx.tnsenterprise.model.dto.FunnelEntDto;
import com.wx.tnsenterprise.model.dto.FunnelRecDto;
import com.wx.tnsenterprise.model.dto.PageDto;
import com.wx.tnsenterprise.model.po.EnterprisePo;
import com.wx.tnsenterprise.model.po.HrPo;
import com.wx.tnsenterprise.model.po.RecruitPo;
import com.wx.tnsenterprise.model.vo.*;
import com.wx.tnsenterprise.service.EnterpriseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 林建权
 * @version 1.0
 * @date 2020/7/28 17:36
 */
@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    @Resource
    private EnterpriseMapper enterpriseMapper;

    @Override
    public List<EnterprisePo> findAll() {
        return enterpriseMapper.findAll();
    }


    @Override
    public PageInfo<EnterprisePo> findAllPageInfo(PageDto pageDto) {
        if (pageDto.getPage()==null){
            pageDto.setPage(1);
        }
        if (pageDto.getPageSize()==null){
            pageDto.setPageSize(10);
        }
        PageHelper.startPage(pageDto.getPage(),pageDto.getPageSize());
        List<EnterprisePo> list=enterpriseMapper.findAll();
        PageInfo<EnterprisePo> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public EnterprisePo findById(Integer id) {
        return enterpriseMapper.findById(id);
    }

    @Override
    public int deleteList(String list) {
        int result = 0;
        String[] ids = list.split(",");
        for (String id:ids){
            int i = delete(Integer.valueOf(id));
            result += i;
        }
        return result;
    }

    @Override
    public int insert(EnterprisePo enterprisePo) {
        return enterpriseMapper.insert(enterprisePo);
    }

    @Override
    public int update(EnterprisePo enterprisePo) {
        return enterpriseMapper.update(enterprisePo);
    }

    @Override
    public int delete(Integer id) {
        return enterpriseMapper.delete(id);
    }

    //首页热门企业
    @Override
    public List<EntHotVo> findHotEnt(Integer length) {
        List<EnterprisePo> hotEnt = enterpriseMapper.findHotEnt(length);
        List<EntHotVo> list = new ArrayList<>();
        for (EnterprisePo ent:hotEnt){
            EntHotVo entHotVo = new EntHotVo();
            BeanUtils.copyProperties(ent,entHotVo);
            entHotVo.setEntId(ent.getId());
            list.add(entHotVo);
        }

        return list;
    }


    //公司栏目搜索公司
    @Override
    public PageInfo<EntNavVo> findEntByFunnel(FunnelEntDto funnelEntDto){
//        System.out.println(funnelEntDto.toString());
        //数据处理成希望的数据
        funnelEntDto = handleFunnel(funnelEntDto);

        PageHelper.startPage(funnelEntDto.getPage(),funnelEntDto.getPageSize());
        List<EntNavVo> list=enterpriseMapper.findEntByFunnel(funnelEntDto);
        PageInfo<EntNavVo> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    //栏目下热门企业
    @Override
    public List<String> findRecHotEnt(Integer length) {
        if (length==null){
            length = 10;
        }
        return enterpriseMapper.findRecHotEnt(length);
    }

    //公司详情页
    @Override
    public EntDetailVo entDetail(Integer id) {


        //详情顶部
        EntDetailTopVo entDetailTopVo = enterpriseMapper.findEntDetailTop(id);
        //详情页热招职位
        List<EntDetailHotRecVo> entDetailHotRecVo = enterpriseMapper.findEntDetailHotRec(id);
        if (entDetailHotRecVo.size()>=3){
            entDetailHotRecVo = entDetailHotRecVo.subList(0,3);
        }
        //详情页公司信息
        EntDetailBriefVo entDetailBriefVo = enterpriseMapper.findEntDetailBrief(id);
        //详情页公司产品
        List<EntDetailProductVo> entDetailProductVo = enterpriseMapper.findEntDetailProduct(id);
        //详情页公司高管信息
        List<EntDetailManagerVo> entDetailManagerVo =enterpriseMapper.findEntDetailManager(id);
        //详情页hr信息(sql复杂，决定拆分)
            //获得公司下所有hr
        List<EntDetailHrVo> entDetailHrVo = enterpriseMapper.findEntDetailHr(id);
            //拼接属性
        for (EntDetailHrVo hrVo:entDetailHrVo){
            //recJobNum
            hrVo.setRecJobNum(enterpriseMapper.findEntDetailRecJobNum(hrVo.getHrId()));
            //recTitle
            hrVo.setRecTitle(enterpriseMapper.findEntDetailRecTitle(hrVo.getHrId()));
        }
            //只显示四个
        if (entDetailHrVo.size()>=4){
            entDetailHrVo = entDetailHrVo.subList(0,4);
        }
        //详情页在招职位(做分页)
        PageHelper.startPage(1,15);
        List<EntDetailRecVo> entDetailRecVo = enterpriseMapper.findEntDetailRec(id);
        PageInfo pageInfo = new PageInfo(entDetailRecVo);


        //封装对象
        EntDetailVo entDetailVo = new EntDetailVo();
        entDetailVo.setEntDetailTopVo(entDetailTopVo);
        entDetailVo.setEntDetailHotRecVo(entDetailHotRecVo);
        entDetailVo.setEntDetailBriefVo(entDetailBriefVo);
        entDetailVo.setEntDetailProductVo(entDetailProductVo);
        entDetailVo.setEntDetailManagerVo(entDetailManagerVo);
        entDetailVo.setEntDetailHrVo(entDetailHrVo);
        entDetailVo.setPageInfo(pageInfo);


        return entDetailVo;
    }


    //处理
    private FunnelEntDto handleFunnel(FunnelEntDto funnelEntDto){
        if ("全国".equals(funnelEntDto.getEntAddr())){
            funnelEntDto.setEntAddr(null);
        }
        if (funnelEntDto.getEntJobId()!=null && funnelEntDto.getEntJobId()<=0){
            funnelEntDto.setEntJobId(null);
        }
        if ("不限".equals(funnelEntDto.getEntJobName())){
            funnelEntDto.setEntJobName(null);
        }
        if ("不限".equals(funnelEntDto.getEntStageFinace())){
            funnelEntDto.setEntStageFinace(null);
        }
        if ("不限".equals(funnelEntDto.getEntStageScale())){
            funnelEntDto.setEntStageScale(null);
        }
        if (funnelEntDto.getPageSize()==null){
            funnelEntDto.setPageSize(28);
        }
        if (funnelEntDto.getPage()==null){
            funnelEntDto.setPage(1);
        }
        return funnelEntDto;
    }

}

